﻿Imports System.Data.OleDb

Public Class ProgressPage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            Dim oleDbConn1 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            oleDbConn1.Open()
            Dim SqlStringLike As String = "select * from tblProgressLikes where ProgressID = @f1 and wlUsername = @f2"
            Dim cmdLikeQ As OleDbCommand = New OleDbCommand(SqlStringLike, oleDbConn1)
            cmdLikeQ.CommandType = CommandType.Text
            cmdLikeQ.Parameters.AddWithValue("@f1", Request.Params("PID"))
            cmdLikeQ.Parameters.AddWithValue("@f2", User.Identity.Name)
            Dim drQ = cmdLikeQ.ExecuteReader
            If drQ.HasRows Then
                'btnLike.Visible = False
                'lblLike.Visible = True

                imgBtnLike.Visible = False
                imgYouLike.Visible = True


            Else
                'btnLike.Visible = True
                'lblLike.Visible = False

                imgBtnLike.Visible = True
                imgYouLike.Visible = False

            End If
        End If





        If IsPostBack = False Then
            If Request.Params("PID") = "" Then Response.Redirect("Default.aspx")
            Dim oleDbConn2 As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim SqlString2 As String = "select * from tblComments where Progress_FKID = @f1"
            Dim cmd1 As OleDbCommand = New OleDbCommand(SqlString2, oleDbConn2)
            cmd1.CommandType = CommandType.Text
            cmd1.Parameters.AddWithValue("@f1", Request.Params("PID"))
            oleDbConn2.Open()
            Dim dataReader = cmd1.ExecuteReader
            grdComments.DataSource = dataReader
            grdComments.DataBind()


            Dim ProgressViewSQL As String = "insert into tblProgressViews (wlUserName, Progress_FKID) values (@f1, @f2)"
            Dim cmdViews As OleDbCommand = New OleDbCommand(ProgressViewSQL, oleDbConn2)
            cmdViews.CommandType = CommandType.Text
            If User.Identity.IsAuthenticated Then
                cmdViews.Parameters.AddWithValue("@f1", User.Identity.Name)
            Else
                cmdViews.Parameters.AddWithValue("@f1", "Anonymous")
            End If

            cmdViews.Parameters.AddWithValue("@f1", Request.Params("PID"))
            cmdViews.ExecuteNonQuery()

        End If

        If User.Identity.IsAuthenticated Then
            phCommentBox.Visible = True
            phRating.Visible = True
            phLogintoComment.Visible = False
            phLike.Visible = True
        Else
            phCommentBox.Visible = False
            phRating.Visible = False
            phLogintoComment.Visible = True
            phLike.Visible = False
        End If


        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim SqlString As String = "select * from AvgProgressRating where ProgressID = @f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("PID"))
        oleDbConn.Open()

        Dim dr As OleDbDataReader = cmd.ExecuteReader()

        If dr.HasRows Then
            dr.Read()
            If Not IsDBNull(dr("AvgRating")) Then rtAverage.CurrentRating = dr("AvgRating")
        End If

    End Sub

    Protected Sub btnAddComment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddComment.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into tblComments(Addedby,Title,Details,DateAdded,Progress_FKID) Values (@f1,@f2,@f3,@f4,@f5)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", txtCTitle.Text)
        cmd.Parameters.AddWithValue("@f3", txtComment.Text)
        cmd.Parameters.AddWithValue("@f4", Date.Today)
        cmd.Parameters.AddWithValue("@f5", Request.Params("PID"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        'Fill grdComments

        Dim fillComments As String = "select * from tblComments where Progress_FKID = @f1"
        Dim fillCommentsCmd As OleDbCommand = New OleDbCommand(fillComments, oleDbConn)
        fillCommentsCmd.CommandType = CommandType.Text
        fillCommentsCmd.Parameters.AddWithValue("@f1", Request.Params("PID"))
        Dim dataReader = fillCommentsCmd.ExecuteReader
        grdComments.DataSource = dataReader
        grdComments.DataBind()


        txtCTitle.Text = ""
        txtComment.Text = ""

    End Sub

    Protected Sub btnTagProgress_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnTagProgress.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into tblTagsProgressJunction(Progress_FKID,Tag_FKID) Values (@f1,@f2)"
        oleDbConn.Open()


        For Each cb As ListItem In cbTags.Items
            If cb.Selected Then
                ' Add Join to tblProgressByTag 

                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("PID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

        If txtNewTags.Text = "" Then Return

        Dim newTags = txtNewTags.Text.Split(",")
        For Each newTag In newTags
            ' First Add the new Tag to the Tags Table
            Dim newTagSQL As String = "insert into tblTags (Tag) values (@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSQL, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()

            ' Then Find the ID of the New Tag
            Dim getLastIDCmd = New OleDbCommand("select top 1 ID from tblTags order by ID Desc", oleDbConn)
            Dim dr = getLastIDCmd.ExecuteReader()
            dr.Read()
            Dim lastID = dr.GetValue(0)

            'Associate this last ID with the Progress ID..
            Dim newCmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            newCmd.CommandType = CommandType.Text
            newCmd.Parameters.AddWithValue("@f1", Request.Params("PID"))
            newCmd.Parameters.AddWithValue("@f2", lastID)
            newCmd.ExecuteNonQuery()

        Next
    End Sub

    Protected Sub btnSaveRating_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSaveRating.Click
        Dim OleDBConn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        OleDBConn.Open()

        Dim delRatingSQL As String = "delete from tblProgressRating where ProgressID=@f1 and wlUserName = @f2"
        Dim delRatingsCmd As New OleDbCommand(delRatingSQL, OleDBConn)
        delRatingsCmd.Parameters.AddWithValue("@f1", Request.Params("PID"))
        delRatingsCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        delRatingsCmd.ExecuteNonQuery()


        Dim AddRatingSQL As String = "insert into tblProgressRating (ProgressID, wlUserName, Rating) values (@f1,@f2, @f3)"
        Dim AddRatingsCmd As New OleDbCommand(AddRatingSQL, OleDBConn)
        AddRatingsCmd.Parameters.AddWithValue("@f1", Request.Params("PID"))
        AddRatingsCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        AddRatingsCmd.Parameters.AddWithValue("@f3", rtProgress.CurrentRating)
        AddRatingsCmd.ExecuteNonQuery()
        lblRatingResult.Text = "Thank You for Adding your Rating!"
    End Sub

    'Protected Sub btnLike_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLike.Click
    '    Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
    '    Dim SqlAddLike As String = "Insert into tblProgressLikes(ProgressID, wlUserName) Values (@f1,@f2)"
    '    Dim cmdAddLike As OleDbCommand = New OleDbCommand(SqlAddLike, oleDbConn)
    '    cmdAddLike.CommandType = CommandType.Text
    '    cmdAddLike.Parameters.AddWithValue("@f1", Request.Params("PID"))
    '    cmdAddLike.Parameters.AddWithValue("@f2", User.Identity.Name)
    '    oleDbConn.Open()
    '    cmdAddLike.ExecuteNonQuery()
    '    btnLike.Visible = False
    '    lblLike.Visible = True
    'End Sub

    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgBtnLike.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim SqlAddLike As String = "Insert into tblProgressLikes(ProgressID, wlUserName) Values (@f1,@f2)"
        Dim cmdAddLike As OleDbCommand = New OleDbCommand(SqlAddLike, oleDbConn)
        cmdAddLike.CommandType = CommandType.Text
        cmdAddLike.Parameters.AddWithValue("@f1", Request.Params("PID"))
        cmdAddLike.Parameters.AddWithValue("@f2", User.Identity.Name)
        oleDbConn.Open()
        cmdAddLike.ExecuteNonQuery()
        imgBtnLike.Visible = False
        imgYouLike.Visible = True
    End Sub
End Class